<?php

  $WorkTable = &$_SQL_TABLE[$GlobPart];
  $WorkTableKeyVarName = $CONFIG['keyVarPrefix'].$WorkTableKeyFieldName;

  $TabFields['n'] = getFieldNamesWithLangs($WorkTable, array('title','body'));
  $TabFields['at'] = getFieldNamesWithLangs($_SQL_TABLE['author'], array('name'));
  //$TabFields['r'] = getFieldNamesWithLangs($_SQL_TABLE['region'], array('title'));
  $TabFields['rb'] = getFieldNamesWithLangs($_SQL_TABLE['rubric'], array('title'));
  $TabFields['k'] = getFieldNamesWithLangs($_SQL_TABLE['keyword'], array('title'));
  $TabFields['b'] = getFieldNamesWithLangs($_SQL_TABLE['block'], array('title'));
  $TabFields['nt'] = getFieldNamesWithLangs($_SQL_TABLE['news_type'], array('title'));

  $AuthorsSelect = array_kv(getTableAsArray($_SQL_TABLE['author'], $TabFields['at']['name'], array(), '', 'id,'.$TabFields['at']['name']), $TabFields['at']['name'], 'id');
  $NewsTypesSelect = array_kv(getTableAsArray($_SQL_TABLE['news_type'], $TabFields['nt']['title'], array(), '', 'id,'.$TabFields['nt']['title']), $TabFields['nt']['title'], 'id');

  if($_REQUEST['mode'] == $GlobPart)
  {
    $PageTitle = '$language["admin"]["news"]." :: ".$Item["'.$TabFields['n']['title'].'"];';
    $RowItem = getRowByField($WorkTable, $WorkTableKeyFieldName, $_GET[$WorkTableKeyVarName]);
    //$RegionsSelectItem = array_kv(getTableAsArray($_SQL_TABLE['region'], $TabFields['r']['title'], array("id IN ('".str_replace($CONFIG['AdminListInRowDelim'], "','", $RowItem['region'])."')"), '', 'id,'.$TabFields['r']['title']), $TabFields['r']['title'], 'id');
    $RubricsSelectItem = array_kv(getTableAsArray($_SQL_TABLE['rubric'], $TabFields['rb']['title'], array("id IN ('".str_replace($CONFIG['AdminListInRowDelim'], "','", $RowItem['rubric'])."')"), '', 'id,'.$TabFields['rb']['title']), $TabFields['rb']['title'], 'id');
    $KeywordsSelectItem = array_kv(getTableAsArray($_SQL_TABLE['keyword'], $TabFields['k']['title'], array("id IN ('".str_replace($CONFIG['AdminListInRowDelim'], "','", $RowItem['keywords'])."')"), '', 'id,'.$TabFields['k']['title']), $TabFields['k']['title'], 'id');
    $BlocksSelectItem = array_kv(getTableAsArray($_SQL_TABLE['block'], $TabFields['b']['title'], array("news_block = 'y'","id IN ('".str_replace($CONFIG['AdminListInRowDelim'], "','", $RowItem['block'])."')"), '', 'id,'.$TabFields['b']['title']), $TabFields['b']['title'], 'id');
    $ImportantRubricsSelectItem = array_kv(getTableAsArray($_SQL_TABLE['rubric'], $TabFields['rb']['title'], array("id IN ('".str_replace($CONFIG['AdminListInRowDelim'], "','", $RowItem['important_rubric'])."')"), '', 'id,'.$TabFields['rb']['title']), $TabFields['rb']['title'], 'id');
    $ImportantBlocksSelectItem = array_kv(getTableAsArray($_SQL_TABLE['block'], $TabFields['b']['title'], array("news_block = 'y'","id IN ('".str_replace($CONFIG['AdminListInRowDelim'], "','", $RowItem['important_block'])."')"), '', 'id,'.$TabFields['b']['title']), $TabFields['b']['title'], 'id');

    $OldDatePath = date("Y/m/d/",strtotime($RowItem['p_date']));
    $NewDatePath = date("Y/m/d/",strtotime($_POST['p_date']));
  }
  else
  {
  	$OldDatePath = '';
    $NewDatePath = date("Y/m/d/",strtotime($_POST['p_date']));

  	//$RegionsSelect = array_kv(getTableAsArray($_SQL_TABLE['region'], $TabFields['r']['title'], array(), '', 'id,'.$TabFields['r']['title']), $TabFields['r']['title'], 'id');
  	$RubricsSelect = array_kv(getTableAsArray($_SQL_TABLE['rubric'], $TabFields['rb']['title'], array(), '', 'id,'.$TabFields['rb']['title']), $TabFields['rb']['title'], 'id');
  	$BlocksSelect = array_kv(getTableAsArray($_SQL_TABLE['block'], $TabFields['b']['title'], array("news_block = 'y'"), '', 'id,'.$TabFields['b']['title']), $TabFields['b']['title'], 'id');

  	$PageTitle = '$language["admin"]["news"];';
  	$AloneMode = $GlobPart;

  	$Query['FromTables'] = 	$WorkTable." n
  							LEFT JOIN ".$_SQL_TABLE['author']." at ON at.id = n.author
  							LEFT JOIN ".$_SQL_TABLE['news_type']." nt ON nt.id = n.news_type
  							LEFT JOIN ".$_SQL_TABLE['news_param']." np ON np.news_id = n.id
    ";
  	$Query['Fields'] = "n.*, nt.".$TabFields['nt']['title']." AS news_type, at.".$TabFields['at']['name']." AS author, IF(np.rating_count, CONCAT(ROUND(np.rating_sum/np.rating_count,1),' (',np.rating_count,')'), '0 (0)') AS rating";
  	$Query['TabOrder'] = "n.";
  	$Query['Where'] = "";
  	$Query['GroupBy'] = "";

  	$EnableFilter = true;

  	$listInfo['where']['date_from'] = array(
  								'simple' => 'y',
  								//'SQLField' => "n.p_date >= '".$CONFIG['AdminFilterValuePat']."'",
  								'type' => 'date',
  								'title' => $language['admin']['dateFrom'],
  								'JSact' => '',
  								'startYear' => '-2',
								'endYear' => '+2',
   								);

    $listInfo['where']['date_to'] = array(
  								'simple' => 'y',
  								//'SQLField' => "n.p_date <= '".$CONFIG['AdminFilterValuePat']." 23:59:59'",
  								'type' => 'date',
  								'title' => $language['admin']['dateTo'],
  								'JSact' => '',
  								'startYear' => '-2',
								'endYear' => '+2',
   								);

   	$listInfo['where']['fd'] = array(
  								'simple' => 'y',
  								'type' => 'checkbox',
  								'checkedValue' => 'y',
								'checked' => $_GET['fd'],
  								'SQLField' => ((!isBlank($_REQUEST['fd'])) ? "n.p_date >= '".$_REQUEST['date_from']."' AND n.p_date <= '".$_REQUEST['date_to']." 23:59:59'" : "" ),
  								'title' => $language['admin']['useDateFilter'],
  								'textAfterField' => $language['admin']['yes'],
  								'JSact' => '(document.getElementById("filter_fd").checked!="") ? document.getElementById("filter_fd").value : ""',

  								);


  	$listInfo['where']['author1'] = array(
  								'simple' => 'y',
  								'SQLField' => "n.author = '".$CONFIG['AdminFilterValuePat']."'",
  								'type' => 'select',
  								'title' => $language['admin']['author'],
  								'values' => array('' => $language['admin']['all']) + $AuthorsSelect,
  								'JSact' => '',
  								'newRow' => 'y',
   								);

   	$listInfo['where']['news_type1'] = array(
  								'simple' => 'y',
  								'SQLField' => "n.news_type = '".$CONFIG['AdminFilterValuePat']."'",
  								'type' => 'select',
  								'title' => $language['admin']['newsType'],
  								'values' => array('' => $language['admin']['all']) + $NewsTypesSelect,
  								'JSact' => '',
   								);

    /*$listInfo['where']['region1'] = array(
  								'simple' => 'y',
  								'SQLField' => "FIND_IN_SET('".$CONFIG['AdminFilterValuePat']."',n.region) > 0",
  								'type' => 'select',
  								'title' => $language['admin']['region'],
  								'values' => array('' => $language['admin']['all']) + $RegionsSelect,
  								'JSact' => '',
   								);*/

   	$listInfo['where']['rubric1'] = array(
  								'simple' => 'y',
  								'SQLField' => "FIND_IN_SET('".$CONFIG['AdminFilterValuePat']."',n.rubric) > 0",
  								'type' => 'select',
  								'title' => $language['admin']['rubric'],
  								'values' => array('' => $language['admin']['all']) + $RubricsSelect,
  								'JSact' => '',
   								);

   	$listInfo['where']['block1'] = array(
  								'simple' => 'y',
  								'SQLField' => "FIND_IN_SET('".$CONFIG['AdminFilterValuePat']."',n.block) > 0",
  								'type' => 'select',
  								'title' => $language['admin']['block'],
  								'values' => array('' => $language['admin']['all']) + $BlocksSelect,
  								'JSact' => '',
   								);

    require_once __CFG_PATH_CODE_ADMIN."admin.filter.inc";
  }

  $ConfLangArr = array(
				'title_' => array(
								'type' => 'input',
								'title' => $language['admin']['title'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST,
								'useInList' => $CONFIG['useInListSort'],
								'required' => $CONFIG['AdminReqPatAll'],
								'makeSameValue' => 'linkname',
								'makeSameValueFrom' => 'title_'.$CONFIG['SiteLanguage'],
								'makeSameValueFunc' => 'makeLinkName(this.value.toLowerCase())',
								'maxlength' => '255',
								'size' => '60',
								),

				'description_' => array(
								'type' => 'fckeditor',
								'title' => $language['admin']['description'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST,
								'SmartyMods' => array('unescape'),
								),

				'body_' => array(
								'type' => 'fckeditor',
								'title' => $language['admin']['text'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST,
								'SmartyMods' => array('unescape'),
								),

				'meta_keywords_' => array(
								'type' => 'input',
								'title' => $language['admin']['metaKeywords'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST,
								//'required' => $CONFIG['AdminReqPatAll'],
								'maxlength' => '255',
								'size' => '60',
								),

				'meta_description_' => array(
								'type' => 'input',
								'title' => $language['admin']['metaDescription'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST,
								//'required' => $CONFIG['AdminReqPatAll'],
								'maxlength' => '255',
								'size' => '60',
								),

				'meta_title_' => array(
								'type' => 'input',
								'title' => $language['admin']['metaTitle'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST,
								//'required' => $CONFIG['AdminReqPatAll'],
								'maxlength' => '255',
								'size' => '60',
								),
				);

  $GeneratedLangArr = getAdminConfigArrayWithLangs($ConfLangArr, $SITE_LANGS);

  $LinkName = make_linkname($_POST['linkname']);
  $_SQL_TABLE_FIELDS[$GlobPart] = $GeneratedLangArr + array(

				'linkname' => array(
								'type' => 'input',
								'title' => $language['admin']['linkName'],
								'addVariable' => $LinkName,
								'useInAddForm' => 'y',
								'unique' => 'y',
								'useInList' => $CONFIG['useInListSort'],
								'useInListEdit' => 'y',
								'required' => $CONFIG['AdminReqPatLinkName'],
								'textUnderField' => '<span class="red">'.$language['admin']['latinAlphAttention'].'</span>',
								'maxlength' => '255',
								'size' => '60',
								'inListSmartyMods' => array("truncate:30:'...':true"),
								),

				'rating' => array(
								'type' => 'value',
								'title' => $language['admin']['rating'],
								'notUsedInDB' => 'y',
								'useInList' => $CONFIG['useInListSort'],
								'orderby' => 'rating_sum/np.rating_count,n.'.$TabFields['n']['title'],
								'tabord' => 'np.',
								),

				'keywords' => array(

								'type' => 'relation_field',
								'title' => $language['admin']['keywords'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['keywords'],
								'required' => $CONFIG['AdminReqPatAll'],
								'listValues' => $KeywordsSelectItem,
								'listToField' => 'y',
								'size' => '255',
								'width' => '500',
								'height' => '500',
								'maxlength' => '255',
								'other' => 'disabled',
								'openLink' => '',
								'openLinkParams' => array(
													'mode' => 'get_values',
													'elemid' => 'keywords',
													'tsk' => 'get_keywords',
													'cntonly' => 'y',
													'resize_win' => 'y',
													'currval' => '"+document.getElementById("keywords").value+"',
										),
								),

				'author' => array(
								'type' => 'select',
								'title' => $language['admin']['author'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['author'],
								'useInList' => $CONFIG['useInListSort'],
								'required' => $CONFIG['AdminReqPatAll'],
								'values' => $AuthorsSelect,
								'orderby' => $TabFields['at']['name'].',n.'.$TabFields['n']['title'],
								'tabord' => 'at.',
								),

				'news_type' => array(
								'type' => 'select',
								'title' => $language['admin']['newsType'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['news_type'],
								'useInList' => $CONFIG['useInListSort'],
								'required' => $CONFIG['AdminReqPatAll'],
								'values' => $NewsTypesSelect,
								'orderby' => $TabFields['nt']['title'].',n.'.$TabFields['n']['title'],
								'tabord' => 'nt.',
								),

				/*'region' => array(

								'type' => 'relation_field',
								'title' => $language['admin']['region'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['region'],
								//'required' => $CONFIG['AdminReqPatAll'],
								'listValues' => $RegionsSelectItem,
								'listToField' => 'y',
								'size' => '20',
								'width' => '500',
								'height' => '500',
								'maxlength' => '20',
								'other' => 'disabled',
								'openLink' => '',
								'openLinkParams' => array(
													'mode' => 'get_values',
													'elemid' => 'region',
													'tsk' => 'get_regions',
													'cntonly' => 'y',
													'resize_win' => 'y',
													'currval' => '"+document.getElementById("region").value+"',
										),
								),*/

				'rubric' => array(

								'type' => 'relation_field',
								'title' => $language['admin']['rubric'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['rubric'],
								//'required' => $CONFIG['AdminReqPatAll'],
								'listValues' => $RubricsSelectItem,
								'listToField' => 'y',
								'size' => '20',
								'width' => '500',
								'height' => '500',
								'maxlength' => '20',
								'other' => 'disabled',
								'openLink' => '',
								'openLinkParams' => array(
													'mode' => 'get_values',
													'elemid' => 'rubric',
													'tsk' => 'get_rubrics',
													'cntonly' => 'y',
													'resize_win' => 'y',
													'currval' => '"+document.getElementById("rubric").value+"',
										),
								),

				'block' => array(

								'type' => 'relation_field',
								'title' => $language['admin']['block'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['block'],
								//'required' => $CONFIG['AdminReqPatAll'],
								'listValues' => $BlocksSelectItem,
								'listToField' => 'y',
								'size' => '20',
								'width' => '500',
								'height' => '500',
								'maxlength' => '20',
								'other' => 'disabled',
								'openLink' => '',
								'openLinkParams' => array(
													'mode' => 'get_values',
													'elemid' => 'block',
													'tsk' => 'get_blocks',
													'cntonly' => 'y',
													'resize_win' => 'y',
													'news_type' => '"+document.getElementById("news_type").value+"',
													'currval' => '"+document.getElementById("block").value+"',
										),
								),

				'important_rubric' => array(

								'type' => 'relation_field',
								'title' => $language['admin']['importantInRubric'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['important_rubric'],
								//'required' => $CONFIG['AdminReqPatAll'],
								'listValues' => $ImportantRubricsSelectItem,
								'listToField' => 'y',
								'size' => '20',
								'width' => '500',
								'height' => '500',
								'maxlength' => '20',
								'other' => 'disabled',
								'openLink' => '',
								'openLinkParams' => array(
													'mode' => 'get_values',
													'elemid' => 'important_rubric',
													'tsk' => 'get_rubrics',
													'cntonly' => 'y',
													'resize_win' => 'y',
													'currval' => '"+document.getElementById("important_rubric").value+"',
													'rubric' => '"+document.getElementById("rubric").value+"',
										),
								),

				'important_block' => array(

								'type' => 'relation_field',
								'title' => $language['admin']['importantInBlock'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['important_block'],
								//'required' => $CONFIG['AdminReqPatAll'],
								'listValues' => $ImportantBlocksSelectItem,
								'listToField' => 'y',
								'size' => '20',
								'width' => '500',
								'height' => '500',
								'maxlength' => '20',
								'other' => 'disabled',
								'openLink' => '',
								'openLinkParams' => array(
													'mode' => 'get_values',
													'elemid' => 'important_block',
													'tsk' => 'get_blocks',
													'cntonly' => 'y',
													'resize_win' => 'y',
													'currval' => '"+document.getElementById("important_block").value+"',
													'block' => '"+document.getElementById("block").value+"',
													'news_type' => '"+document.getElementById("news_type").value+"',
										),
								),

				'active' => array(
								'type' => 'select_link',
								'title' => $language['admin']['status'],
								'formid' => $CONFIG['activeFormName'],
								'addVariable' => 'y',
								'noUseInEdit' => 'y',
								'useInList' => $CONFIG['useInListSort'],
								'inListEdit' => 'select_link',
								'values' => array(
											'y' => array(
													'title' => $language['admin']['active'],
													'className' => 'active',
													'formFields' => array(
																	'act' => 'status',
																	'varvalue' => 'n',
																	'varname' => 'active',
																	getKeyVarName() => '{$Item.$WorkTableKeyFieldName}',
													),
											),
											'n' => array(
													'title' => $language['admin']['inactive'],
													'className' => 'inactive',
													'formFields' => array(
																	'act' => 'status',
																	'varvalue' => 'y',
																	'varname' => 'active',
																	getKeyVarName() => '{$Item.$WorkTableKeyFieldName}',
													),
											),
								),
				),

				'date' => array(
								'type' => 'value',
								'title' => $language['admin']['date'],
								'addVariable' => 'NOW()',
								'addVarType' => $CONFIG['VarTypeSQLFunction'],
								'useInList' => $CONFIG['useInListSort'],
								'useInListEdit' => 'y',
								),

				'p_date' => array(
								'type' => 'datetime',
								'title' => $language['admin']['publicationDate'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['p_date'],
								'useInList' => $CONFIG['useInListSort'],
								'startYear' => '-2',
								'endYear' => '+2',
								),

				/*'a_date' => array(
								'type' => 'datetime',
								'title' => $language['admin']['archivingDate'],
								'useInAddForm' => 'y',
								'addVariable' => $_POST['a_date'],
								'useInList' => $CONFIG['useInListSort'],
								'startYear' => '-2',
								'endYear' => '+2',
								'defaultValue' => (time() + 3600*24*$CONFIG['NewsDaysCountToArchive']),
								),*/

				'ftp_image_logo' => array(
								'type' => 'image',
								'old_filename' => $RowItem['id'],
								'filetype' => $CONFIG['imFileType'],
								'filename' => $RowItem['id'],
								//'filenamePrefix' => $CONFIG['imLogoDefix'],
								'old_dirname' => __CFG_PATH_IMAGES_URL.$CONFIG['NewsImageFolder'].$OldDatePath,
								'dirname' => __CFG_PATH_IMAGES_URL.$CONFIG['NewsImageFolder'].$NewDatePath,
								'docroot_dirname' => __CFG_PATH_IMAGES_URL.$CONFIG['NewsImageFolder'].$OldDatePath,
								'title' => $language['admin']['image'],
								'useInAddForm' => 'y',
								'notUsedInDB' => 'y',
								//'noResize' => 'y',
								'useFTP' => 'y',
								//'remoteServerLink' => $CONFIG['FTPImageServerURL'],
								'ftpServer' => __CFG_PTF_HOSTNAME,
								'ftpUserName' => __CFG_PTF_USERNAME,
								'ftpPassword' => __CFG_PTF_PASSWORD,
								'ftpRootPath' => __CFG_PTF_CORE_PATH,
								'sizes' => array(
										$CONFIG['NewsSmallImagePrefix'] => $CONFIG['NewsSmallImageSize'],
										$CONFIG['NewsNormalImagePrefix'] => $CONFIG['NewsNormalImageSize'],
										//$CONFIG['NewsOriginalImagePrefix'] => '',
										),
								),

				'file' => array(
								'type' => 'file',
								'title' => $language['admin']['fileLink'],
								'addVariable' => $_POST['file'],
								'useInAddForm' => 'y',
								'size' => 45,
								),

  );

  $_SQL_TABLE_EDIT_FORMS[$GlobPart] = array('active');

  function addActionsGetItem()
  {    global $Item, $_SQL_TABLE;

    $Params = getRowByField($_SQL_TABLE['news_param'], 'news_id', $Item['id']);
    $Item['rating'] = ($Params['rating_count'] > 0) ? round($Params['rating_sum'] / $Params['rating_count'],1)." (".$Params['rating_count'].")" : "0 (0)";
  }

?>